package com.jiaai.web.zfp_back.member;


import com.alibaba.dubbo.config.annotation.Reference;
import com.jiaai.pay.IPayAPI;
import com.jiaai.pay.ITxApi;
import com.jiaai.pay.entitiy.MoneyQueryVO;
import com.jiaai.pay.entitiy.TixianExcelVO;
import com.jiaai.util.RestResultGenerator;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;

/**
 * class_name:ZBPayController
 * describe:支付提现
 * create_user:张昉平
 * create_date:2017/12/20
 * create_date:11:52
 */
@RestController
@RequestMapping("/api/v1/admin/pay")
public class ZBPayController {

    @Reference
    private IPayAPI iPayAPI;
    @Reference
    private ITxApi txApi;

    //充值列表
    @GetMapping("/list")
    public Object list(@RequestParam Integer curr, MoneyQueryVO vo){

        return RestResultGenerator.genSuccessResult(iPayAPI.getAll(curr, vo));
    }
    //充值订单刷新
    @PostMapping("/payRefresh")
    public Object payRefresh(@RequestParam String orderNo){
        if (iPayAPI.refresh(orderNo)){
            return RestResultGenerator.genSuccessResult("大爷的, ok了");
        }else {
            return RestResultGenerator.genErrorResult("大爷的, 不ok");
        }
    }

    //提现列表
    @GetMapping("/txList")
    public Object txList(@RequestParam Integer curr, MoneyQueryVO vo){
        return RestResultGenerator.genSuccessResult(txApi.getAll(curr, vo));

    }
    //审核
    @PostMapping("/check")
    public Object check(@RequestParam String txId, @RequestParam Integer status,@RequestParam Integer glod ,@RequestParam String mId){
        if (txApi.checkPass(txId, status,glod,mId)){
            return RestResultGenerator.genSuccessResult("大爷的, ok了");
        }else {
            return RestResultGenerator.genErrorResult("大爷的, 不ok");
        }
    }

    /**
     * 刷新提现订单
     * @param txId
     * @return
     */
    @PostMapping("/refresh")
    public Object refresh(@RequestParam String txId){
        if (txApi.refreshTxOrder(txId)){
            return RestResultGenerator.genSuccessResult("大爷的, ok了");
        }else {
            return RestResultGenerator.genErrorResult("大爷的, 不ok");
        }
    }

//
//    @GetMapping("/excel")
//    public void excel(HttpServletResponse response){
//        JSONArray ja = JSONArray.parseArray(JSONArray.toJSONString(iPayAPI.getAll(null, null)));
//
//        Map<String,String> headMap = new LinkedHashMap<String,String>();
//        headMap.put("nickName","会员昵称");
//        headMap.put("mId","会员编号");
//        headMap.put("transaction_id","第三方支付");
//        headMap.put("settlement_code","结算状态");
//        headMap.put("channel","支付方式");
//        headMap.put("err_code","错误代码");
//        headMap.put("err_code_des","错误描述");
//        headMap.put("old_gold","无折金币");
//        headMap.put("settlement_time","结算日期");
//        headMap.put("price_unit","货币单位");
//        headMap.put("gold","对应金币");
//        headMap.put("out_trade_no","订单号");
//        headMap.put("price","订单金额");
//        headMap.put("result_code","业务结果");
//        headMap.put("time","开始时间");
//        headMap.put("time_end","结束时间");
//        headMap.put("status","订单状态");
//
//        String title = "充值记录-至".concat(new SimpleDateFormat("YYYY-MM-dd HH:mm:ss").format(new Date()));
//
//        ExcelUtil.downloadExcelFile(title,headMap,ja,response);
//    }

    /**
     * =============================================后台api==============================================================
     */
    /**
     * 获取excel文件名
     * @param tixianExcelVO
     * @return
     */
    @GetMapping("/excelname")
    public Object getExcelName(TixianExcelVO tixianExcelVO){
        try {
            System.out.println("111111111111111111111111111111111111111111");
            return RestResultGenerator.genSuccessResult(txApi.getAllExcl(tixianExcelVO));
        } catch (Exception e) {
            throw new RuntimeException("下载失败");
        }
    }

    /**
     * 下载Excel文件
     * @param request
     * @param response
     * @param filename
     */
    @GetMapping("/excelOrder")

    public void getExcel(HttpServletRequest request, HttpServletResponse response, @RequestParam String filename){
        String fullPath = "D://upload//jiaai//excel//".concat(filename);
        File downloadFile = new File(fullPath);
        ServletContext context = request.getServletContext();

        String mimeType = context.getMimeType(fullPath);
        mimeType = "application/octet-stream; charset=utf-8";

        response.setContentType(mimeType);
        response.setContentLength((int) downloadFile.length());

        String headerKey = "Content-Disposition";
        String headerValue = null;

        try {
            headerValue = "attachment; filename=".concat(new String("商品信息表.xlsx".getBytes(), "ISO-8859-1"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        response.setHeader(headerKey, headerValue);
        // Copy the stream to the response's output stream.
        try {
            InputStream myStream = new FileInputStream(fullPath);
            IOUtils.copy(myStream, response.getOutputStream());
            response.flushBuffer();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


}
